package com.lzx.iteam.adapter;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.CloudGroup;
import io.rong.imageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by Megan on 15/6/26.
 */
public class ChooseCloudGroupAdapter extends BaseAdapter {

    public static final int ITEM_TYPE_NOTICE = 101;
    //    private static final int ITEM_TYPE_MORE = 102;
    public static final int ITEM_TYPE_CLOUD_GROUP_ITEM = 103;

    private ArrayList<CloudGroup> mCloudGroupList = new ArrayList<CloudGroup>();
    private boolean mShowCloudGroupNoticeItem = false;
    private String mCloudGroupNoticeItemText = null;
    private LayoutInflater mLayoutInflater;
    //    private CloudDialerActivity mContext;
    private Activity mContext;

    public ChooseCloudGroupAdapter(Activity c, ImageLoader imageLoader) {
        mContext = c;
        mLayoutInflater = LayoutInflater.from(c);
    }


    public ArrayList<CloudGroup> getCloudGroupList() {
        return mCloudGroupList;
    }


    public void updateCloudGroupNoticeItem(String text) {
        mCloudGroupList.clear();
        mShowCloudGroupNoticeItem = true;
        mCloudGroupNoticeItemText = text;
        notifyDataSetChanged();
    }

    public void updateCloudGroupList(ArrayList<CloudGroup> cloudGroupList) {
        mCloudGroupList.clear();
        mCloudGroupList.addAll(cloudGroupList);
//        mCloudGroupList = cloudGroupList;
        mShowCloudGroupNoticeItem = false;
        notifyDataSetChanged();
    }

    public void clearCloudGroupList() {
        mCloudGroupList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mShowCloudGroupNoticeItem) {
            return ITEM_TYPE_NOTICE;
        } else {
            return ITEM_TYPE_CLOUD_GROUP_ITEM;
        }
    }

    @Override
    public int getCount() {
        if (mShowCloudGroupNoticeItem) {
            return 1;
        }
        return (mCloudGroupList != null ? mCloudGroupList.size() : 0);
    }

    @Override
    public CloudGroup getItem(int position) {
        if (ITEM_TYPE_CLOUD_GROUP_ITEM == getItemViewType(position)) {
            return mCloudGroupList != null ? mCloudGroupList.get(position) : null;
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (ITEM_TYPE_CLOUD_GROUP_ITEM == getItemViewType(position)) {
            ViewHodler hodler = null;
            final CloudGroup groupItem = getItem(position);
            if (convertView == null || convertView.getTag() == null) {
                hodler = new ViewHodler();
                convertView = mLayoutInflater.inflate(R.layout.choose_cloud_group_item, null);
                hodler.groupName = (TextView) convertView.findViewById(R.id.group_choose_name);
                hodler.groupUseMsg = (TextView) convertView.findViewById(R.id.group_choose_use_msg);
                convertView.setTag(hodler);
            } else {
                hodler = (ViewHodler) convertView.getTag();
            }
            hodler.groupName.setText(groupItem.groupName);
            hodler.groupUseMsg.setText(groupItem.contactCount + "个联系人 (" + groupItem.memberCount + "人在用)");

            return convertView;
        } else {
            return bindCloudGroupNoticeItem(parent);
        }
    }


    static class ViewHodler {
        TextView groupName;
        TextView groupUseMsg;
    }

    private View bindCloudGroupNoticeItem(ViewGroup parent) {
        View convertView = mLayoutInflater.inflate(R.layout.title, parent, false);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        title.setText(mCloudGroupNoticeItemText);
        title.setGravity(Gravity.CENTER);
        title.setBackgroundResource(R.color.white);
        return convertView;
    }
}
